define(['vue','iview','api'], function (Vue,iView,api) {
    Vue.use(iView)
    var bus = new Vue();
    //添加职位按钮弹框面板
    Vue.component('button-pannel', {
        template: "#button-pannel-template",
        data: function () {
            return {
                saveLoading: false,
                /*添加职位开始=========*/
                addPosition: false,
                /*职业默认角色*/
                roles: [
                    {
                        value: '管理层',
                        label: '管理层'
                    },
                    {
                        value: '员工',
                        label: '员工'
                    }
                ],

                /*工薪等级*/
                scale: [
                    {
                        value: '高级',
                        label: '高级'
                    },
                    {
                        value: '中级',
                        label: '中级'
                    },
                    {
                        value: '低级',
                        label: '低级'
                    },

                ],

                /*职位职称*/
                title: [
                    {
                        value: '总经理',
                        label: '总经理'
                    },
                    {
                        value: '产品',
                        label: '产品'
                    },
                    {
                        value: '市场',
                        label: '市场'
                    },

                ],
                jobInfo: {
                    number: '',
                    roles: '',
                    scale: '',
                    title: '',
                    jobPost: '',
                    basicSalary: ''
                },
                tempAddJobData: {},
                /*添加职位结束=========*/
            }
        },
        methods: {
            handleAddUser: function () {
                var that = this
                this.saveLoading = true
                api.PostJobAdd(this.jobInfo)
                    .done(function (res) {
                        if (res.IsSuccess) {
                            bus.$emit('on-add-post', res.ResultObject)
                            that.$Message.success(res.ErrMsg)
                        } else {
                            that.$Message.error(res.ErrMsg)
                        }
                        that.handleCancel()
                    })
                    .fail(function () {
                        that.$Message.error(res.ErrMsg)
                    })
                    .always(function () {
                        that.saveLoading = false
                    })
            },
            handleCancel: function () {
                this.addPosition = false
                this.jobInfo = {
                    number: '',
                    roles: '',
                    scale: '',
                    title: '',
                    jobPost: '',
                    basicSalary: ''
                }
            }
        }
    });

//表格内容
    Vue.component('table-pannel', {
        template: "#table-pannel-template",
        created: function () {
            var that = this
            bus.$on('on-add-post', function (res) {
                that.tableData.push(res)
            })
            this.initTableData()
            this.initColumns()
        },
        data: function () {
            return {
                saveLoading: false,
                delLoading: false,
                /*添加职位开始=========*/
                addPosition: false,
                /*职业默认角色*/
                roles: [
                    {
                        value: '管理层',
                        label: '管理层'
                    },
                    {
                        value: '员工',
                        label: '员工'
                    }
                ],
                /*工薪等级*/
                scale: [
                    {
                        value: '高级',
                        label: '高级'
                    },
                    {
                        value: '中级',
                        label: '中级'
                    },
                    {
                        value: '低级',
                        label: '低级'
                    },

                ],
                /*职位职称*/
                title: [
                    {
                        value: '总监',
                        label: '总监'
                    },
                    {
                        value: '产品',
                        label: '产品'
                    },
                    {
                        value: '程序员',
                        label: '程序员'
                    },

                ],
                /*添加职位结束=========*/

                //删除职位开始=========
                deletePos: false,
                //删除职位结束=========

                jobInfo: {
                    number: '',
                    roles: '',
                    scale: '',
                    title: '',
                    jobPost: '',
                    basicSalary: ''
                },
                //表格数据
                columns4: [],
                tableData: [],
                delNumber: '',
                tempEditData: {}

            }
        },
        methods: {
            // 初始化数据
            initTableData: function () {
                var that = this;
                api.fetchJobList()
                    .done(function (res) {
                        if (res.IsSuccess) {
                            that.tableData = res.ResultObject
                        } else {
                            that.$Message.error(res.ErrMsg)
                        }
                    })
            },
            // 用户编辑框
            handleEditUser: function () {
                var that = this
                this.saveLoading = true
                api.PostJobEdit(this.jobInfo)
                    .done(function (res) {
                        if (res.IsSuccess) {
                            that.tableData.forEach(function (t, index) {
                                if (t.number === res.ResultObject.number) {
                                    that.tableData[index].roles = res.ResultObject.roles
                                    that.tableData[index].scale = res.ResultObject.scale
                                    that.tableData[index].title = res.ResultObject.title
                                    that.tableData[index].jobPost = res.ResultObject.jobPost
                                    that.tableData[index].basicSalary = res.ResultObject.basicSalary
                                }
                            })
                            that.$Message.success(res.ErrMsg)
                        } else {
                            that.$Message.error(res.ErrMsg)
                        }
                        that.handleCancel()
                    })
                    .fail(function () {
                        that.$Message.error(res.ErrMsg)
                    })
                    .always(function () {
                        that.saveLoading = false
                    })

            },
            handleCancel: function () {
                this.addPosition = false
                this.deletePos = false
                this.jobInfo = {
                    number: '',
                    roles: '',
                    scale: '',
                    title: '',
                    jobPost: '',
                    basicSalary: ''
                }
            },
            // 删除弹窗保存按钮
            handleSuccessDel: function () {
                var that = this
                this.delLoading = true
                api.PostJobDel(this.delNumber)
                    .done(function (res) {
                        if (res.IsSuccess) {
                            that.tableData.forEach(function (t, index) {
                                if (t.number === that.delNumber) {
                                    that.tableData.splice(index, 1)
                                }
                            });
                            that.$Message.success(res.ErrMsg)
                        } else {
                            that.$Message.error(res.ErrMsg)
                        }
                        that.handleCancel()
                    })
                    .fail(function (res) {
                        that.$Message.error(res.ErrMsg)
                    })
                    .always(function () {
                        that.delLoading = false
                    })
            },
            initColumns: function () {
                var that = this
                this.columns4 = [

                    {
                        title: '编号',
                        key: 'number',
                    },
                    {
                        title: '职位名称',
                        key: 'jobPost',
                    },
                    {
                        title: '操作',
                        key: 'handle',
                        render: (function (h, params) {
                            return h('div', [
                                h('Tooltip', {
                                    attrs: {
                                        placement: 'top',
                                        content: '编辑'
                                    }
                                }, [
                                    h('span', {
                                        attrs: {
                                            class: 'editStaff',
                                            type: 'primary'
                                        },
                                        on: {
                                            click: function () {
                                                that.jobInfo.number = params.row.number
                                                that.jobInfo.roles = params.row.roles
                                                that.jobInfo.scale = params.row.scale
                                                that.jobInfo.title = params.row.jobTitle
                                                that.jobInfo.jobPost = params.row.jobPost
                                                that.jobInfo.basicSalary = params.row.basicSalary
                                                that.addPosition = true
                                            }
                                        }
                                    }, [
                                        h('img', {
                                            attrs: {
                                                src: '../../images/edit-defaut.png'
                                            }
                                        })
                                    ])
                                ]),
                                h('Tooltip', {
                                    attrs: {
                                        placement: 'top',
                                        content: '删除'
                                    }
                                }, [
                                    h('span', {
                                        attrs: {
                                            class: 'cancelStaff',
                                            type: 'primary'
                                        },
                                        on: {
                                            click: function () {
                                                that.delNumber = params.row.number
                                                that.deletePos = true
                                            }
                                        }
                                    }, [
                                        h('img', {
                                            attrs: {
                                                src: '../../images/move-defaut.png'
                                            }
                                        })
                                    ])
                                ])
                            ])
                        })
                    },
                ]
            }
        }
    });
    var app = new Vue({
        el: "#app",


    })
})
